<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Resume extends Controller
{

    public function _initialize()
    {
        if(!Session::has('Username')){
            $this->error('请登录!','../');
        }
    }
    public function index()
    {
    	$id = Session::get('UserId');
    	$map['id'] = $id;
    	$list = Db::name('resume')->where($map)->select();
        $data = explode('/n/',$list[0]['filenames']);
        array_pop($data);
        $this->assign('list', $list);
    	$this->assign('data', $data);
    	return $this->fetch('resume');
    }

    public function lists()
    {
        $id = Session::get('UserId');
        $map['id'] = $id;
        $list = Db::name('jobjoin')->where($map)->cache(true,10)->paginate(10);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch('list');   
    }

    public function uploadresume()
    {
    	$param = request()->param();
		$param['id'] = Session::get('UserId');
    	$fileimg = request()->file('header');
    	if($fileimg){
	    	//图片转存
	    	$info = $fileimg->move(ROOT_PATH . 'public' . DS . 'uploads');
		    if($info){
	            $param['avater'] = DS.'public'.DS.'uploads'.DS.$info->getSaveName();
		    }else{
		        // 上传失败获取错误信息
	            return json(['message'=>$file->getError()]);
		    }
    	}
        $data = Db::name('resume')->where('id',$param['id'])->select();
	    $param['filenames'] = $data[0]['filenames'];
    	$files = request()->file('file');
    	if($files){
	    	//多文件上传处理
	    	foreach($files as $key => $file){
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$param['email'],'');
		        if($info){
		            // 成功上传后 获取上传信息
	            	$param['filenames'] = $param['filenames'].DS.'public'.DS.'uploads'.DS.$param['email'].DS.$info->getSaveName().'/n/';
		        }else{
		            // 上传失败获取错误信息
	            	return json(['message'=>$file->getError()]);
		        }    
	    	}
    	}else{
            unset($param['filenames']);
        }
    	$resume = model('resume');
    	$data = $resume->addresume($param);
    	if($data){
            $this->success('上传成功!请刷新页面，若文件没有正常上传请尝试最新chrome浏览器!');
    		// return json(['message'=>$param]);
    	}else{
            $this->error('上传失败!请联系管理员!');
    		// return json(['message'=>$resume->getError()]);
    	}
    }
    //删除指定的filename
    public function deletefile()
    {
        $param = request()->post();
        $data = Db::name('resume')->where('id',$param['id'])->select();
        // dump($data[0]['filenames']);
        $list = explode('/n/',$data[0]['filenames']);
        array_pop($list);
        unset($list[$param['did']]);
        array_push($list,'');
        $lists = implode("/n/",$list);
        $result = Db::name('resume')->where('id',$param['id'])->update(['filenames'=>$lists]);
        if($result){
            return json(['message'=>'删除成功!']);
        }else{
            return json(['message'=>'删除失败!']);
        }
    }
}
